<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon.ico">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('https://durianyang.gitee.io/blog').hostname,
    root: '/blog/',
    scheme: 'Gemini',
    version: '7.6.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":true},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}
  };
</script>

  <meta name="description" content="在JAVA中需要编码的场景一、在I&#x2F;O操作中存在的编码这个IO包括磁盘IO和网络IO，磁盘的IO编码就是字符和字节的转换过程，在涉及到IO操作时，只要注意指定统一的编解码Charset字符集一般不会出现问题。 二、在内存操作中的编码最常用的就是在内存中进行从字符到字节的数据类型转换： 1String s &#x3D; &quot;这是一段中文字符&quot;;2byte[] b &#x3D; s.getBytes(&quot;UTF-8&quot;);3S">
<meta name="keywords" content="编码,JavaWeb">
<meta property="og:type" content="article">
<meta property="og:title" content="第三章 中文编码问题">
<meta property="og:url" content="https:&#x2F;&#x2F;durianyang.gitee.io&#x2F;blog&#x2F;2019&#x2F;12&#x2F;24&#x2F;%E7%AC%AC%E4%B8%89%E7%AB%A0-%E4%B8%AD%E6%96%87%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98&#x2F;index.html">
<meta property="og:site_name" content="Durian blog">
<meta property="og:description" content="在JAVA中需要编码的场景一、在I&#x2F;O操作中存在的编码这个IO包括磁盘IO和网络IO，磁盘的IO编码就是字符和字节的转换过程，在涉及到IO操作时，只要注意指定统一的编解码Charset字符集一般不会出现问题。 二、在内存操作中的编码最常用的就是在内存中进行从字符到字节的数据类型转换： 1String s &#x3D; &quot;这是一段中文字符&quot;;2byte[] b &#x3D; s.getBytes(&quot;UTF-8&quot;);3S">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-12-24T02:04:26.000Z">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://durianyang.gitee.io/blog/2019/12/24/%E7%AC%AC%E4%B8%89%E7%AB%A0-%E4%B8%AD%E6%96%87%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>第三章 中文编码问题 | Durian blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Durian blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">生活又不是热血动漫</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/Durianyang" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://durianyang.gitee.io/blog/2019/12/24/%E7%AC%AC%E4%B8%89%E7%AB%A0-%E4%B8%AD%E6%96%87%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="Durian">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Durian blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          第三章 中文编码问题
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-24 10:04:26 10:04:26" itemprop="dateCreated datePublished" datetime="2019-12-24T10:04:26+08:00">2019-12-24 10:04:26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>1.2k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>1 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h4 id="在JAVA中需要编码的场景"><a href="#在JAVA中需要编码的场景" class="headerlink" title="在JAVA中需要编码的场景"></a>在JAVA中需要编码的场景</h4><h5 id="一、在I-O操作中存在的编码"><a href="#一、在I-O操作中存在的编码" class="headerlink" title="一、在I/O操作中存在的编码"></a>一、在I/O操作中存在的编码</h5><p>这个IO包括磁盘IO和网络IO，磁盘的IO编码就是字符和字节的转换过程，在涉及到IO操作时，只要注意指定统一的编解码Charset字符集一般不会出现问题。</p>
<h5 id="二、在内存操作中的编码"><a href="#二、在内存操作中的编码" class="headerlink" title="二、在内存操作中的编码"></a>二、在内存操作中的编码</h5><p>最常用的就是在内存中进行从字符到字节的数据类型转换：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span></pre></td><td class="code"><pre><span class="line">String s = <span class="string">"这是一段中文字符"</span>;</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">2</span></pre></td><td class="code"><pre><span class="line"><span class="keyword">byte</span>[] b = s.getBytes(<span class="string">"UTF-8"</span>);</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">3</span></pre></td><td class="code"><pre><span class="line">String n = <span class="keyword">new</span> String(b,<span class="string">"UTF-8"</span>);</span></pre></td></tr></table></figure>

<p>另一种方式是：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span></pre></td><td class="code"><pre><span class="line">Charset charset = Charset.forName(<span class="string">"UTF-8"</span>);</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">2</span></pre></td><td class="code"><pre><span class="line">ByteBuffer byteBuffer = charset.encode(string);<span class="comment">// 字符-&gt;字节</span></span></pre></td></tr><tr><td class="gutter"><pre><span class="line">3</span></pre></td><td class="code"><pre><span class="line">CharBuffer charBuffer = charset.decode(byteBuffer);<span class="comment">// 字节-&gt;字符</span></span></pre></td></tr></table></figure>

<p>字符与字节的转换只要设置的编解码格式一致，一般不会出现乱码问题.</p>
<a id="more"></a>

<h4 id="在Java中如何编解码"><a href="#在Java中如何编解码" class="headerlink" title="在Java中如何编解码"></a>在Java中如何编解码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span></pre></td><td class="code"><pre><span class="line">String str = <span class="string">"I am 中国人"</span>;    <span class="comment">//字符数组的值为: 49 20 61 6d 20 4e2d 56fd 4eba</span></span></pre></td></tr><tr><td class="gutter"><pre><span class="line">2</span></pre></td><td class="code"><pre><span class="line">        <span class="keyword">char</span>[] chars = str.toCharArray();</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">3</span></pre></td><td class="code"><pre><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> aChar : chars)</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">4</span></pre></td><td class="code"><pre><span class="line">        &#123;</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">5</span></pre></td><td class="code"><pre><span class="line">            System.out.print(Integer.toHexString(aChar) + <span class="string">" "</span>);</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">6</span></pre></td><td class="code"><pre><span class="line">        &#125;</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">7</span></pre></td><td class="code"><pre><span class="line">        <span class="comment">// 按照IOS8859-1编码</span></span></pre></td></tr><tr><td class="gutter"><pre><span class="line">8</span></pre></td><td class="code"><pre><span class="line">        System.out.println();</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">9</span></pre></td><td class="code"><pre><span class="line">        <span class="keyword">byte</span>[] bytes = str.getBytes(StandardCharsets.ISO_8859_1);</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">10</span></pre></td><td class="code"><pre><span class="line">        <span class="keyword">for</span> (<span class="keyword">byte</span> aByte : bytes)</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">11</span></pre></td><td class="code"><pre><span class="line">        &#123;</span></pre></td></tr><tr><td class="gutter"><pre><span class="line">12</span></pre></td><td class="code"><pre><span class="line">            System.out.print(Integer.toHexString(aByte) + <span class="string">" "</span>); <span class="comment">// 49 20 61 6d 20 3f 3f 3f ，3f在ISO-8859-1中为?</span></span></pre></td></tr><tr><td class="gutter"><pre><span class="line">13</span></pre></td><td class="code"><pre><span class="line">        &#125;</span></pre></td></tr></table></figure>

<p>1、将“I am 中国人”这段包含中文字符的字符串进行ISO-8859-1编码后，中文被转化的值是3f，也就是ISO中“？”。</p>
<p>2、按照GB2312编码，转换值改变，然后根据转化后的值去码表中找对应的字节。</p>
<p>3、按照GBK编码，与GB2312一致，只是GBK的码表长度更长。</p>
<p>4、按照UTF-16编码，UTF-16将char数组放大了一倍。单字节范围内的字符在高位补0变成两个字节，中文字节也变成两个字节。</p>
<p>5、按照UTf-8编码，单字节范围内的字符仍然用一个字节，而中文采用三个字节表示。</p>
<blockquote>
<p>总结：GBK比GB312能处理更对的汉字。</p>
<p>UTF16比UTF-8编码效率更高，适合在本地磁盘和内存中使用。但是它不适合在网络间传输，</p>
<p>因为网络传输容易损坏字节流，一旦字节流损坏很难恢复，相较而言，UTF-8更适合在网络间传输。</p>
<p>UTF在编码安全和效率上做了平衡，是理想的中文编码。</p>
</blockquote>
<h4 id="JAVAWeb中的编码与解码"><a href="#JAVAWeb中的编码与解码" class="headerlink" title="JAVAWeb中的编码与解码"></a>JAVAWeb中的编码与解码</h4>
    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/%E7%BC%96%E7%A0%81/" rel="tag"><i class="fa fa-tag"></i> 编码</a>
              <a href="/blog/tags/JavaWeb/" rel="tag"><i class="fa fa-tag"></i> JavaWeb</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2019/12/16/%E4%BD%BF%E7%94%A8idea%E6%8F%92%E4%BB%B6plantUML%E7%94%BBUML%E7%B1%BB%E5%9B%BE/" rel="prev" title="使用idea插件plantUML画UML类图">
      <i class="fa fa-chevron-left"></i> 使用idea插件plantUML画UML类图
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2020/01/30/%E9%98%BF%E9%87%8C%E4%BA%91%E9%83%A8%E7%BD%B2easy-mock/" rel="next" title="阿里云部署easy-mock">
      阿里云部署easy-mock <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#在JAVA中需要编码的场景"><span class="nav-number">1.</span> <span class="nav-text">在JAVA中需要编码的场景</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#一、在I-O操作中存在的编码"><span class="nav-number">1.1.</span> <span class="nav-text">一、在I/O操作中存在的编码</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#二、在内存操作中的编码"><span class="nav-number">1.2.</span> <span class="nav-text">二、在内存操作中的编码</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#在Java中如何编解码"><span class="nav-number">2.</span> <span class="nav-text">在Java中如何编解码</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#JAVAWeb中的编码与解码"><span class="nav-number">3.</span> <span class="nav-text">JAVAWeb中的编码与解码</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Durian"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Durian</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">7</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/Durianyang" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;Durianyang" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1323807496@qq.com" title="E-Mail → mailto:1323807496@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        


<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Durian</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">18k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">16 分钟</span>
</div>
<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.0.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.6.0
  </div>
-->

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共7.4k字</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>
<script src="/blog/js/utils.js"></script><script src="/blog/js/motion.js"></script>
<script src="/blog/js/schemes/pisces.js"></script>
<script src="/blog/js/next-boot.js"></script>



  




  <script src="/blog/js/local-search.js"></script>












  

  

</body>
</html>
